using System.Collections;
using System.Collections.Generic;
using GTA;
using MotionFramework.Event;
using UnityEngine;
using UnityEngine.UI;

namespace SevenPieceGame
{
    public class UIAmazingMatch : MonoBehaviour
    {
        private EventGroup EventGrouper = new EventGroup();

        private Text m_AddDollerNumber;
        private Button m_BtnClose;
        private Button m_BtnClaim;
        private Image m_ImgProgress;
        private Text m_TxtProgress;
        private GameObject m_Loading;
        private Text m_WatchVideoMoney;
        
        private int m_ScoreFromLine;
        private void Awake()
        {
            OnCreate();
        }
        
        void OnCreate()
        { 
            m_AddDollerNumber = this.transform.Find("Image/Txt_DollerCount").GetComponent<Text>();
            m_BtnClose = this.transform.Find("Btn_Close").GetComponent<Button>();
            m_BtnClaim = this.transform.Find("Btn_Claim").GetComponent<Button>();
            m_ImgProgress = this.transform.Find("Loading/Img_Loading").GetComponent<Image>();
            m_TxtProgress = this.transform.Find("Loading/Txt_Loading").GetComponent<Text>();
            m_Loading = this.transform.Find("Loading").gameObject;
            m_WatchVideoMoney = this.transform.Find("Loading/Txt_WatchVideoMoney").GetComponent<Text>();
            
            m_BtnClose.onClick.AddListener(OnClickCloseBtn);
            m_BtnClaim.onClick.AddListener(OnClickClaimBtn);
        }

        private T GetUIComponent<T>(string path)
        {
            return this.transform.Find(path).gameObject.GetComponent<T>();
        }

        public void InitData(int scoreFromLine)
        {
            m_ScoreFromLine = scoreFromLine;
            m_AddDollerNumber.text = $"+{m_ScoreFromLine}";

            ShopListItemData _shopItemData = UIShopManager.Instance.GetShowItemData();
            if (_shopItemData == null)
            {
                m_Loading.SetActive(false);
            }
            else
            {
                if (_shopItemData.m_CMCountProgress >= 50)
                {
                    m_Loading.SetActive(false);
                }
                else
                {
                    m_Loading.SetActive(true);
                    m_ImgProgress.fillAmount = (float)_shopItemData.m_CMCountProgress / 50f;
                    m_TxtProgress.text = $"{_shopItemData.m_CMCountProgress}/50";
                    m_WatchVideoMoney.text = $"Watch {50 - _shopItemData.m_CMCountProgress} videos to active your money.";
                }
            }
        }
        
        private void OnClickCloseBtn()
        {
            UserLayerManager.Instance.CheckInstallToLowUser();
            if (UserLayerManager.Instance.GetCurrentUserLayer() == UserLayerManager.UserLayerType.LowUser)
            {
                MainManager.Instance.m_UpdateScore(m_ScoreFromLine);
                if (MainManager.Instance.m_LastShowInterstitialAdTime >= 60)
                {
                    ADSHelper.ShowInterstitialAd("", AdsDataMgr.EInterstitiaFireScene.Seven);
                }
            }
            MainManager.Instance.HideUIAmazingMatch();
        }

        private void OnClickClaimBtn()
        {
            GTA.ADSHelper.ShowRewardedVideoAd((status) =>
            {
                AdjustWrapper.Instance.LogEvent("kffdcz", null);
                MainManager.Instance.m_UpdateScore(m_ScoreFromLine * 3);
                MainManager.Instance.HideUIAmazingMatch();
            }, ERVType.SevenPiece.ToString());
        }
    }
}


